package com.zuqiukong.automaticpacking.taskheadler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.zuqiukong.automaticpacking.Constants;
import com.zuqiukong.automaticpacking.ProcessUtils;
import com.zuqiukong.automaticpacking.ProcessUtils.LineMsgHandle;
import com.zuqiukong.automaticpacking.model.Model;
import com.zuqiukong.automaticpacking.pojo.ChannelPojo;

/**
 * 任务逻辑处理器
 *
 * @author jie
 */
public class ChannelTask {

    private String channelId;
    protected boolean buildSuccessful;
//    public ChannelTask() {
//
//    }
    public ChannelTask(String channelId) {
        this.channelId = channelId;

    }

    public void doWork() {

        assembleRelease();
    }

    /**
     * 还原源码并更新到最新版本
     */
    private void checkoutAndUpdate() {
        String[] cmdCheckoutSource = {"cmd", "/c", "git", "-C", Constants.PROJECT_CHECK_VERSION_PATH, "checkout", "master", "-f"};
        String[] cmdUpdateSource = {"cmd", "/c", "git", "-C", Constants.PROJECT_CHECK_VERSION_PATH, "pull"};
        try {
            ProcessUtils.exec(cmdCheckoutSource, "还原master源码", null);
            ProcessUtils.exec(cmdUpdateSource, "更新master到最新版本", null);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 进行打包
     */
    private void assembleRelease() {
        Model.getInstance().updateStatus(channelId, ChannelPojo.STATUS_PROCESSING);
        String[] cmdPacking = {"cmd", "/c", "E:/GithubProject/NoCardPay/gradlew.bat", "-p", "E:/GithubProject/NoCardPay/", "assembleRelease"};
        try {
            buildSuccessful = false;
            ProcessUtils.exec(cmdPacking, "master打发布包", new LineMsgHandle() {
                @Override
                public void handleLine(String line) {
                    if (line.indexOf("BUILD SUCCESSFUL") != -1) {
                        buildSuccessful = true;
                    }
                }
            });

            if (buildSuccessful) {
                Model.getInstance().updateStatus(channelId, ChannelPojo.STATUS_SUCCESS);
                Constants.log("打包完成");
                putFile();
            } else {
                Model.getInstance().updateStatus(channelId, ChannelPojo.STATUS_FAILED);
                Constants.log("打包失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 把打包文件放置到下载目录
     */
    private void putFile() {
        String apkName = "/NOCARD.apk";
        String apkPackgaPath = Constants.PROJECT_CHECK_VERSION_PATH + Constants.APK_PATH + apkName;
        apkPackgaPath = apkPackgaPath.replaceAll("/", "\\\\");
        String WebPath = Constants.WebPath.replace("/", "\\\\");
        Constants.log("apkPackgaPath:" + apkPackgaPath);
        Constants.log("WebPath:" + Constants.WebPath);
        String[] cmdCopyApk = {"cmd", "/c", "copy", apkPackgaPath, WebPath, "/y"};
        try {
            ProcessUtils.exec(cmdCopyApk, "拷贝apk到web目录", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新数据库
     */
    private void updateModel() {

    }

}
